﻿Imports System.IO
Imports Telerik.WinControls.UI

Public Class DwgList

    Private Sub DwgList_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.RadDropDownList1.Items.Clear()
        Me.RadDropDownList2.Items.Clear()

        dtCreate.Reset()
        SelectStr = <string>
select sp_id,name,dir_path from xxxSchemaName.T_PLANTGROUP t where t.depth='xxxdepth' order by t.dir_path
                    </string>.Value.Replace("xxxdepth", MaxDepth)
        MyPIDConn.PIDReadDataByDBA(MyPIDConn.PIDPlant_DBNAME, MyPIDConn.PIDPlant_User, SelectStr, dtCreate)
        For Each unit In dtCreate.Rows
            Dim newenum As New RadListDataItem
            newenum.Text = unit("dir_path").ToString
            newenum.Tag = unit("name").ToString
            Me.RadDropDownList1.Items.Add(newenum)
        Next

        '拿template路径
        SelectStr = <string>select value from xxxSchemaName.T_OPTIONSETTING t
            where t.name = 'PID Template Path'
                                     </string>.Value
        dtCreate.Reset()
        MyPIDConn.PIDReadDataByDBA(MyPIDConn.PIDPlant_DBNAME, MyPIDConn.ActivePlantPIDApp_User, SelectStr, dtCreate)
        Dim folder As New DirectoryInfo(dtCreate.Rows(0)(0).ToString())
        For Each template In folder.GetFiles("*.pid")
            Dim newenum As New RadListDataItem
            newenum.Text = template.Name
            newenum.Tag = template.Name
            Me.RadDropDownList2.Items.Add(newenum)
        Next


    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim x As New PIDContents.PIDContents
        If Me.RadDropDownList1.SelectedIndex = -1 Then
            MsgBox("请选择一个图纸目录所在的unit")
            Exit Sub
        End If
        If Me.RadDropDownList2.SelectedIndex = -1 Then
            MsgBox("请选择一个图纸目录的图框")
            Exit Sub
        End If
        Dim unit = Me.RadDropDownList1.SelectedItem.Tag.ToString
        Dim template = Me.RadDropDownList2.SelectedItem.Text.ToString
        If Me.TextBox3.Text = "" Or Me.TextBox4.Text = "" Then
            MsgBox("图纸目录的name和number不能为空")
            Exit Sub
        End If
        Dim name = Me.TextBox3.Text.ToString
        Dim number = Me.TextBox4.Text.ToString

        If DrawingCol.Count = 0 Then
            MsgBox("图纸范围为0")
            Exit Sub
        End If
        Try
            Dim DwgNamecol As New ArrayList
            Dim ids As String = ""
            For Each dwgid In DrawingCol
                'DwgNamecol.Add(DS.GetDrawing(dwgid).Name.ToString)
                ids += "'" + dwgid + "',"
            Next
            ids = ids.Substring(0, Len(ids) - 1)
            Dim dt As New DataTable
            Dim sql As String = <string>
select t.name,t.title,t.drawingnumber from xxxSchemaName.t_drawing t
where t.sp_id in (xxxids)                            
order by t.name
                                </string>.Value.Replace("xxxids", ids)
            MyPIDConn.PIDReadDataByDBA(MyPIDConn.PIDPlant_DBNAME, MyPIDConn.ActivePlantPIDApp_User, Sql, dt)
            x.CreateDwgContent(dt, unit, template, name, number)
            Dim audit As New audit(DS.ProjectNumber, "图纸目录", "V1.0")
            audit.Add()
            audit = Nothing
            Me.Hide()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        LoadDrawing(True)
    End Sub

    Private Sub DwgList_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        Console.WriteLine("1")
        '得到选择的图纸
        UnitCol.Clear() : DrawingCol.Clear()
        LoopTreeNode(RadForm1.RadTreeView1.Nodes.Item(0), DrawingCol, UnitCol)
        Me.TextBox1.Text = DrawingCol.Count
    End Sub
End Class
